package com.hb.trade.server.project.homepage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.homepage.dto.AnnouncementsDTO;
import com.hb.core.pojo.project.homepage.entity.Announcements;
import com.hb.trade.server.project.homepage.service.AnnouncementsService;
import com.hb.trade.server.project.util.DateUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 首页公告通知 前端控制器
 * </p>
 *
 * @author ljs
 * @since 2022-2-18 15:16:19
 */
@RestController
public class AnnouncementsController {
    @Autowired
    private AnnouncementsService announcementsService;

    /**
     * 分页查询系统公告
     *
     * @param announcementsDTO
     * @return
     */
    @PostMapping("/home/announcementsList")
    public Message<PageResult<Announcements>> announcementsList(@RequestBody AnnouncementsDTO announcementsDTO) {
        QueryWrapper<Announcements> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","title","content", "read_flag","create_time");
        queryWrapper.orderByDesc("create_time");
        Page<Announcements> announcementsPage = announcementsService.page(announcementsDTO.pages(), queryWrapper);
        return MsgUtil.success(PageResult.get(announcementsPage));
    }

    /**
     * 查询系统公告详情
     *
     * @param announcementsDTO
     * @return
     */
    @PostMapping("/home/announcementsDetail")
    public Message<AnnouncementsDTO> announcementsDetail(@RequestBody AnnouncementsDTO announcementsDTO) {
        if (announcementsDTO == null || announcementsDTO.getId() == null) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM_TYPE.msg());
        }
        Announcements announcements = announcementsService.getById(announcementsDTO.getId());
        // 标记为已读
        announcements.setReadFlag(1);
        Long num = announcements.getClickCount();
        announcements.setClickCount(num+1);
        AnnouncementsDTO dto = announcements.to(AnnouncementsDTO.class);
        if (announcements.getCreateTime() != null){
//            dto.setPublishDate(DateUtil.dateFormat(announcements.getCreateTime()));
            dto.setPublishDate(announcements.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        boolean result = announcementsService.updateById(announcements);
        return MsgUtil.success(dto);
    }

    /**
     * 保存或者更新系统公告
     *
     * @param announcementsDTO
     * @return
     */
    @PostMapping("/home/AnnouncementsSave")
    public Message AnnouncementsSave(@RequestBody AnnouncementsDTO announcementsDTO) {
        if (announcementsDTO == null || announcementsDTO.getTitle() == null) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM_TYPE.msg());
        }
        if (announcementsDTO.getId() == null){
            announcementsDTO.buildBaseData(1);
        }
        boolean result = announcementsService.saveOrUpdate(announcementsDTO);
        return result ? MsgUtil.success() : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 删除系统公告
     *
     * @param announcementsDTO
     * @return
     */
    @PostMapping("/home/AnnouncementsRemove")
    public Message AnnouncementsRemove(@RequestBody AnnouncementsDTO announcementsDTO) {
        if (announcementsDTO == null || announcementsDTO.getId() == null) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM_TYPE.msg());
        }
        boolean result = announcementsService.removeById(announcementsDTO.getId());
        return result ? MsgUtil.success() : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }
}
